package src;
// T-408-STNE
// Operating systems and networks
// Spring 2011
//
// Programming assignment 2: Page replacement algorithms
//
// This is the main simulation class of your project. Modify only as indicated by TODOs.
// Based on code by Olafur R. Helgason

import java.io.*;

public class Memsym 
{


	//
	// runExperiment
	//
	// Runs one set of experiments for a given memory size and a vector of page references.
	//
	public static void runExperiment(int memSize, int pageReferences[])
	{
        long iruntime;

		System.out.println("Running experiment for a " + memSize + " frame memory\n");

        iruntime = System.currentTimeMillis();
		FifoAlgorithm fifoAlg = new FifoAlgorithm(memSize, pageReferences);
		System.out.println("FIFO:    " + Integer.toString(fifoAlg.pageFaults()));
        System.out.println("timer: " + (System.currentTimeMillis() - iruntime) + "\n");

        iruntime = System.currentTimeMillis();
		OptimalAlgorithm optAlg = new OptimalAlgorithm(memSize, pageReferences);
		System.out.println("Optimal: " + Integer.toString(optAlg.pageFaults()));
        System.out.println("timer: " + (System.currentTimeMillis() - iruntime) + "\n");

        iruntime = System.currentTimeMillis();
        SecondChangeAlgorithm segAlg = new SecondChangeAlgorithm(memSize,pageReferences);
        System.out.println("Second Change: " + Integer.toString(segAlg.pageFaults()));
        System.out.println("timer: " + (System.currentTimeMillis() - iruntime) + "\n");

        iruntime = System.currentTimeMillis();
        ClockAlgorithm clockAlg = new ClockAlgorithm(memSize,pageReferences);
        System.out.println("Clock: " + Integer.toString(clockAlg.pageFaults()));
        System.out.println("timer: " + (System.currentTimeMillis() - iruntime)+ "\n");

		System.out.println();
	}

	public static void main(String args[]) throws IOException 
	{
		// Here are the parameters for the generation of virtual memory (page) references.
		// Note that given these parameters, we generate references to pages in a 1024 page
		// virtual memory.
		int n=10;	// Processes
		int m=500;	// Page references per process
		int k=924;	// Base address range
		int t=100;	// Address range relative to base address

		int pageReferences[] = new int[n*m];
		try {

			// Generate some random page references. This is the pages which our program accesses, 
			// i.e. the memory calls made in the code. Note that memory references by our "processes
			// can overlap.
			int idx = 0, rand;
			System.out.println("\nBase values : ");
			// Generate references for n processes
			for (int i = 0; i < n; i++) 
			{
				int baseVal = (int) Math.floor(Math.random() * k);
				System.out.print(Integer.toString(baseVal) + " ");
				// For each of the n processes, generate m references relative to the base address.
				for (int j = 0; j < m; j++) 
				{
			 		rand = (int) (Math.random() * t);
					pageReferences[idx++] = baseVal + rand;
				}
			} 
			System.out.println("\n");

			// 
			// Run the experiment for the memory sizes we want to simulate. Note that memory sizes
			// are in terms of page frames.
			//
			runExperiment(32, pageReferences);
			runExperiment(64, pageReferences);
			runExperiment(128, pageReferences);
			runExperiment(256, pageReferences);
			runExperiment(512, pageReferences);
		}
		catch(Exception e) {
		  System.err.println(e.toString());
		  System.exit(1);
		}

	}
}

